function that calculates the trace of a square matrix
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
real(kind=dp), | intent(in), | DIMENSION(:, :) | :: | A |
FUNCTION Trace(A) RESULT(result) REAL(dp), DIMENSION(:, :), INTENT(IN) :: A REAL(dp) :: result INTEGER :: i, N N = SIZE(A, 1) IF (SIZE(A, 2) /= N) STOP "Error: Matrix must be square." result = SUM([(A(i, i), i=1,N)]) END FUNCTION Trace